﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SumiaAccountBook.SubControls
{
    /// <summary>
    /// UCCurrent.xaml 的交互逻辑
    /// </summary>
    public partial class UCCurrent : UserControl
    {
        const int PageSize = 20;
        private int page_count = 1;

        Biz.KVGoingProject kvProject = new Biz.KVGoingProject();

        public UCCurrent()
        {
            InitializeComponent();

            this.cb_type.ItemsSource = Biz.KVConstHelper.GetInstance().GetAllValue<Biz.KVCurrentType>();
            this.cb_type.SelectedIndex = 0;

            this.cb_project.ItemsSource = this.kvProject.GetAllValue();

            this.dp_date.Text = DateTime.Now.ToString();

            bindData();
            var total_Receivable = DataAccess.DaCurrent.GetTotal((int)Biz.KVCurrentType.emCurrentType.In);
            var total_Payable = DataAccess.DaCurrent.GetTotal((int)Biz.KVCurrentType.emCurrentType.Out);
            this.lb_sm.Text = "应收:" + total_Receivable + ",应付:" + total_Payable;
            this.DataContext = this;

        }

        private void bindData()
        {
            int t1 = 0;
            List<DataAccess.Entity.Current> tmpList = DataAccess.DaCommon<DataAccess.Entity.Current>.GetList(page_count, PageSize, ref t1);
            if (null != tmpList && tmpList.Count > 0)
            {
                List<Biz.CurrentObj> list = new List<Biz.CurrentObj>();
                string type = string.Empty;                
                string project = string.Empty;
                foreach (var entity in tmpList)
                {
                    type = Biz.KVConstHelper.GetInstance().GetValue<Biz.KVCurrentType>(entity.Type);                    
                    project = this.kvProject.GetValue(entity.ProjectId);                    
                    list.Add(
                        new Biz.CurrentObj
                        {
                            Key = entity.Key,
                            Type = type,                            
                            Amount = entity.Amount,
                            Date = entity.Date,
                            Project = project,                            
                            Memo = entity.Memo
                        });
                }

                this.dg.ItemsSource = list;
                this.dp.TotalCount = t1;
            }
        }

        private void btn_add_Click(object sender, RoutedEventArgs e)
        {
            int type = Biz.KVConstHelper.GetInstance().GetKey<Biz.KVCurrentType>(this.cb_type.SelectedValue as string);

            DateTime dt = DateTime.MinValue;
            if (!string.IsNullOrEmpty(this.dp_date.Text.Trim()))
            {
                dt = Convert.ToDateTime(this.dp_date.Text.Trim());
            }
            else
            {
                MessageBox.Show("请填写日期！");
                return;
            }
            
            decimal amount = 0;
            if (!decimal.TryParse(this.tb_amount.Text.Trim(), out amount))
            {
                MessageBox.Show("金额应该填写数字！");
                return;
            }
            
            int ProjectId = -1;
            if (!string.IsNullOrEmpty(this.cb_project.SelectedValue as string))
            {
                ProjectId = this.kvProject.GetKey(this.cb_project.SelectedValue as string);
            }
            
            string memo = this.tb_memo.Text.Trim();

            DataAccess.Entity.Current entity = new DataAccess.Entity.Current
            {
                Type = type,               
                Amount = amount,
                Date = dt,
                ProjectId = ProjectId,
                Memo = memo
            };

            try
            {
                DataAccess.DaCommon<DataAccess.Entity.Current>.Add(entity);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            bindData();
        }

        private void dp_PageChanged(object sender, CubicFamily.StockTool.ComonControl.PageChangedEventArgs e)
        {
            this.page_count = e.CurrentPageIndex;
            bindData();
        }

        private void dg_remove_Click(object sender, RoutedEventArgs e)
        {
            var original = e.OriginalSource as Button;
            var key = original.CommandParameter;
            int Id = Convert.ToInt32(key);
            MessageBoxResult confirmToDel = MessageBox.Show("确认要删除所选行吗？", "提示", MessageBoxButton.YesNo, MessageBoxImage.Question);
            if (confirmToDel == MessageBoxResult.Yes)
            {
                try
                {
                    DataAccess.DaCommon<DataAccess.Entity.Current>.Delete(Id);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

                bindData();
            }
        }
    }
}
